import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/src/rx_flutter/rx_notifier.dart';
import 'package:keep_account/bean/SalaryRecords.dart';
import 'package:keep_account/bean/entry.dart';
import 'package:keep_account/bean/work_records.dart';
import 'package:keep_account/dio/http_request.dart';
import 'package:keep_account/utils/datetime_utils.dart';
import 'package:keep_account/vm/base_controller.dart';
import 'package:keep_account/vm/selection_radio_controller.dart';

import '../dio/DefaultRequestResult.dart';

class YMDWorkRecordsController extends BaseController {



  var salaryEntry = <SalaryRecords>[].obs;
  var radioGroupValue = 10;
  var radioYearValue = 1.obs;
  var radioMonthValue = 2.obs;
  var selectionRadioController = SelectionRadioController();

  @override
  void onReady() {
    super.onReady();
    selectionRadioController.selectIndexStream.listen((event) {
      getWorkRecordsByRangeDate(event);
    });
    getWorkRecordsByRangeDate(1);
  }

  void getWorkRecordsByRangeDate(int type){
    var startDate = "";
    var endDate = "";
    if (type == 0) {
      startDate = (getCurrentYear() - 5).toString();
      endDate = getCurrentYear().toString();
      _getWorkRecordsByYear(startDate, endDate);
    }else if (type == 1) {
      startDate = "${getCurrentYear()}-01";
      endDate = "${getCurrentYear()}-12";
      _getWorkRecordsByMonth(startDate, endDate);
    }else if (type == 2) {
      startDate = "${getCurrentYear()}-${getCurrentMonth().toString().padLeft(2,"0")}-01";
      endDate = "${getCurrentYear()}-${getCurrentMonth().toString().padLeft(2,"0")}-${getDaysInMonth(getCurrentYear(), getCurrentMonth())}";
      _getWorkRecordsByDay(startDate, endDate);
    }else {
      startDate = "${getCurrentYear()}-${getCurrentMonth().intMonthToString()}-01";
      endDate = "${getCurrentYear()}-${getCurrentMonth().toString().padLeft(2,"0")}-${getDaysInMonth(getCurrentYear(), getCurrentMonth())}";
      _getWorkRecordsByDay(startDate, endDate);
    }

  }

  void _getWorkRecordsByYear(String startDate,String endDate){
    HttpRequest().getWorkRecordsRangeYear(
        {
          "startDate": startDate,
          "endDate": endDate
        },
        requestResult: DefaultRequestResult(
            success: (data){
              salaryEntry.clear();
              salaryEntry.addAll( (data as List<dynamic>).map((e) => SalaryRecords.fromJson(e as Map<String,dynamic>)).toList() );
              change(null,status: RxStatus.success());
            },
            emptyData: (){
              change(null,status: RxStatus.empty());
            },
            error: (error){
              EasyLoading.showError(error.message);
              change(null,status: RxStatus.empty());
            }
        ));
  }
  void _getWorkRecordsByMonth(String startDate,String endDate){
    HttpRequest().getWorkRecordsRangeMonth(
        {
          "startDate": startDate,
          "endDate": endDate
        },
        requestResult: DefaultRequestResult(
            success: (data){
              salaryEntry.clear();
              salaryEntry.addAll( (data as List<dynamic>).map((e) => SalaryRecords.fromJson(e as Map<String,dynamic>)).toList() );
              change(null,status: RxStatus.success());
            },
            emptyData: (){
              change(null,status: RxStatus.empty());
            },
            error: (error){
              EasyLoading.showError(error.message);
              change(null,status: RxStatus.empty());
            }
        ));
  }
  void _getWorkRecordsByDay(String startDate,String endDate){
    HttpRequest().getWorkRecordsRangeDay(
        {
          "startDate": startDate,
          "endDate": endDate
        },
        requestResult: DefaultRequestResult(
            success: (data){
              salaryEntry.clear();
              salaryEntry.addAll( (data as List<dynamic>).map((e) => SalaryRecords.fromJson(e as Map<String,dynamic>)).toList() );
              change(null,status: RxStatus.success());
            },
            emptyData: (){
              change(null,status: RxStatus.empty());
            },
            error: (error){
              EasyLoading.showError(error.message);
              change(null,status: RxStatus.empty());
            }
        ));
  }



  @override
  void retry() {
    getWorkRecordsByRangeDate(1);
  }


}